package com.android.scanner.fragment;

import com.android.scanner.R;
import com.android.scanner.utils.Constant;
import com.android.scanner.utils.SysUtil;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class TimeFragment extends BaseFragment implements OnClickListener {
	private Handler handler;
	private Context context;
	private Button btnLeft, btnRight;
	private TextView tvDate, tvDateFormat, tvTime, TVTimeFormat;
	private static TimeFragment mSelft = null;

	public static TimeFragment getInstance(Handler handler) {
		if (mSelft != null)
			return mSelft;
		return new TimeFragment(handler);
	}

	public TimeFragment(Handler handler) {
		this.handler = handler;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		context = getActivity();
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_time, null);
		initViews(view);
		return view;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		// if (adapter != null) {
		// Log.e("kirk", "kirk add - curPosition is " + curPosition);
		// lv.setSelection(curPosition);
		// adapter.updateHLPosition(curPosition);
		// }
		super.onResume();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	private void initViews(View view) {
		// TODO Auto-generated method stub

		btnLeft = (Button) view.findViewById(R.id.btn_left);
		btnRight = (Button) view.findViewById(R.id.btn_right);
		btnLeft.setOnClickListener(this);
		btnRight.setOnClickListener(this);

		tvDate = (TextView) view.findViewById(R.id.tv_time_date);
		tvDateFormat = (TextView) view.findViewById(R.id.tv_time_date_format);
		tvTime = (TextView) view.findViewById(R.id.tv_time_time);
		TVTimeFormat = (TextView) view.findViewById(R.id.tv_time_time_format);

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if (arg0.getId() == R.id.btn_left) {

			handler.sendEmptyMessage(Constant.MSG_BACK_SETTINGS);
		} else if (arg0.getId() == R.id.btn_right) {
			handler.sendEmptyMessage(Constant.MSG_BACK_SETTINGS);
		}
	}

	@Override
	public boolean setKeyEvent(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_MENU) {

			btnLeft.callOnClick();
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {

			btnRight.callOnClick();
		} else if (keyCode == KeyEvent.KEYCODE_F5) {
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
			if (tvDate.isFocused()) {
				SysUtil.setDate(2014, 03, 31);
			} else if (tvTime.isFocused()) {
				SysUtil.setTime(12, 24);
			}
		}
		return super.setKeyEvent(keyCode, event);
	}

}
